﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class CommentWrapperCollection
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorksheetCommentsPartWrapper"/>.
		/// </summary>
		public WorksheetCommentsPartWrapper WorksheetCommentsPart
		{
			get { return (WorksheetCommentsPartWrapper)Parent.Parent.Parent; }
		}

		/// <summary>
		/// Gets the associated <see cref="WorksheetPartWrapper"/>.
		/// </summary>
		public WorksheetPartWrapper WorksheetPart
		{
			get { return (WorksheetPartWrapper)WorksheetCommentsPart.Parent; }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns whether this collection contains a comment wrapper having the specified row and column indexes.
		/// </summary>
		/// <param name="address">The grid address of the target comment.</param>
		/// <returns>A value that indicates whether a matching comment wrapper exists.</returns>
		internal bool Contains(
			GridReference address)
		{
			return Find(address) != null;
		}

		/// <summary>
		/// Returns the wrapper for the comment having the specified row and column indexes.
		/// </summary>
		/// <param name="address">The grid address of the target comment.</param>
		/// <returns>A <see cref="CommentWrapper"/> that matches the specified address. <c>null</c> if no match is found.</returns>
		internal CommentWrapper Find(
			GridReference address)
		{
			return Items.Find(item => item.CellAddress.RowIndex == address.RowIndex && item.CellAddress.ColumnIndex == address.ColumnIndex);
		}

		/// <summary>
		/// Returns a <see cref="CommentWrapper"/> that wraps a new <see cref="Comment"/>.
		/// </summary>
		/// <param name="address">The grid address of the target comment.</param>
		/// <returns>A new instance of <see cref="RowWrapper"/>.</returns>
		internal CommentWrapper Add(
			GridReference address)
		{
			var result = Add();
			result.CellAddress.RowIndex = address.RowIndex;
			result.CellAddress.ColumnIndex = address.ColumnIndex;
			result.CellAddress.RowAbsolute = false;
			result.CellAddress.ColumnAbsolute = false;
			result.Activate();
			return result;
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom persistence functionality.
		/// </summary>
		protected override void OnCustomRecomposing()
		{
			// The row list must be in reference sequence before persistence
			Items.Sort((a, b) => a.CellAddress.CompareTo(b.CellAddress));
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
